package fantasy;

import java.io.IOException;
import java.net.URL;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gdata.client.DocumentQuery;
import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthException;
import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.util.ResourceNotFoundException;
import com.google.gdata.util.ServiceException;

public class GDocFileExistsServlet extends HttpServlet {
	
	private DocsService client = new DocsService("dhpollack-dhpollack944-v6");
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		/*
		UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		GOAuth g = new GOAuth();
		try {
			log("Getting Google Access Tokens for " + user.getNickname());
			Key k = new KeyFactory.Builder(GOAuth.class.getSimpleName(), user.getNickname()).getKey();
			g = pm.getObjectById(GOAuth.class, k);
		} catch (JDOObjectNotFoundException e) {
			log("User hasn't done the OAuth dance");
			return;
		}
		
		String ACCESS_TOKEN = g.getAccessToken();
		String TOKEN_SECRET = g.getAccessTokenSecret();
		*/
		
		GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
		oauthParameters.setOAuthConsumerKey("dhpollack944.appspot.com");
		oauthParameters.setOAuthConsumerSecret("5sLdLvnBuYa+fpcWdzie9ndr");
		oauthParameters.setOAuthToken("1/_g_qJAVneAqWqcxUOk7qQcVzicS9POsRC9KQ1XoJ-Dk");
		oauthParameters.setOAuthTokenSecret("d4ot7JkTXqVgw6LnvYNq8lqF");
		
		try {
			client.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
		} catch (OAuthException e) {
			// TODO Auto-generated catch block
			log("OAuthException: " + e.toString());
			e.printStackTrace();
			return;
		}
		
		URL url = new URL("https://docs.google.com/feeds/default/private/full/spreadsheet:0Agx922rWdElkdnY4S241LUNDck12Z3dwdERDMVE");
		
		try {
			DocumentListEntry entry = client.getEntry(url, DocumentListEntry.class);
			resp.getWriter().println(entry.getId());

		} catch (ResourceNotFoundException e) {
			log("Google Doc Not Found");
			
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			log("Service Broke");
			e.printStackTrace();
		}
		

		

		
	}


}
